package com.gopay.common.cps.dao.sms.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.sms.SmsMassContentDAO;
import com.gopay.common.domain.sms.SmsMassContent;
import com.gopay.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SmsMassContentDAOImpl extends BaseDAOImpl<SmsMassContent,String> implements SmsMassContentDAO{

    @Override
    public String getNextSq() {
        String id = getNextId(SmsMassContent.SMS_MASS_CONTENT_SEQ);
        id = StringUtils.leftPad(id, 8, "0");
        id =  "RE"+DateUtils.now2string("yyyyMMdd") + id;
        return id;
    }

    @Override
    public SmsMassContent selectMass(String key){
        List<SmsMassContent> massContents = findByHql("from SmsMassContent where smsId='" + key + "'");
        if(massContents!=null && massContents.size()==1){
            return massContents.get(0);
        }
        return null;
    }

    @Override
    public List<SmsMassContent> selectTheSmsWillBeSend() {
        StringBuffer sql = new StringBuffer();
        sql.append(" from SmsMassContent ");
        sql.append(" where sendTime >= to_date(:sendTime, 'yyyyMMdd') AND  sendTime < to_date(:sendTime, 'yyyyMMdd')+1 AND status='0' ORDER BY sendTime ASC ");
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sendTime",DateUtils.format(new Date(), "yyyyMMdd"));
        return findByHql(sql.toString(),paramMap);
    }

    @Override
    public void updateMassSatus(String status,String smsId) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("status",status);
        paramMap.put("smsId",smsId);
        paramMap.put("lastUpdTs",new Date());
        updateByHql(" update SmsMassContent set status=:status , lastUpdTs=:lastUpdTs where smsId=:smsId ",paramMap);
    }

    @Override
    public List<SmsMassContent> selectTheSmsCompensate() {
        StringBuffer sql = new StringBuffer();
        sql.append(" from SmsMassContent ");
        sql.append(" where sendTime >= to_date(:sendTime, 'yyyyMMdd') AND  sendTime < to_date(:sendTime, 'yyyyMMdd')+1 AND status='3' ORDER BY sendTime ASC ");
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sendTime",DateUtils.format(new Date(), "yyyyMMdd"));
        return findByHql(sql.toString(),paramMap);
    }

    @Override
    public List<SmsMassContent> selectTheSmsWillBeSendByPage(int pageNum, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append(" from SmsMassContent ");
        sql.append(" where sendTime >= to_date(:sendTime, 'yyyyMMdd') AND  sendTime < to_date(:sendTime, 'yyyyMMdd')+1 AND status='0' ORDER BY sendTime ASC ");
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sendTime",DateUtils.format(new Date(), "yyyyMMdd"));
        return findByHql(sql.toString(),paramMap,pageNum,pageSize);
    }
}